//
//  MainViewController.swift
//  阿恺微博
//
//  Created by 孙嘉恺 on 16/10/6.
//  Copyright © 2016年 孙嘉恺. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    //懒加载属性只会创建一次
    lazy var composeButton: UIButton = UIButton(title: nil, image: "tabbar_compose_icon_add", backgroundImage: "tabbar_compose_button", target: self, action: #selector(compose(button:)))
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        
        //设置tabbar的颜色
        //tabBar.tintColor = UIColor.orange
        //添加所有的子控制器
        addChildViewControllers()
        
        //设置tabbar的线条
        setTabarLine()
        
        //print(tabBar.subviews)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        addComposeButton()
        //print(tabBar.subviews)
    }
}


// MARK: - UI搭建
extension MainViewController {
    /// 添加所有的子控制器
    func addChildViewControllers () {
        let home = HomeViewController()
        addChildViewController(controller: home, image: "tabbar_home", title: "首页")
        
        let message = MessageViewController()
        addChildViewController(controller: message, image: "tabbar_message_center", title: "消息")
        
        //占位的controller, 目的是让tabbarItem 平均分布
        let placeHolder = UIViewController()
        addChildViewController(controller: placeHolder, image: "", title: "")
        
        let discover = DiscoverViewController()
        addChildViewController(controller: discover, image: "tabbar_discover", title: "发现")
        
        let profile = ProfileViewController()
        addChildViewController(controller: profile, image: "tabbar_profile", title: "我")
    }
    
    /// 添加单个子控制器
    ///
    /// - parameter controller: 子控制器
    /// - parameter image:      item的image的名字
    /// - parameter title:      title
    func addChildViewController(controller: UIViewController, image: String, title: String) {
        
        controller.title = title
        
        //如果不设置tintColor, 希望以原图显示
        controller.tabBarItem.image = UIImage(named: image)?.withRenderingMode(.alwaysOriginal)
        controller.tabBarItem.selectedImage = UIImage(named: "\(image)_selected")?.withRenderingMode(.alwaysOriginal)
        
        //设置tabbar的颜色和字体
        controller.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 11), NSForegroundColorAttributeName: UIColor.darkGray], for: .normal)
        controller.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 11), NSForegroundColorAttributeName: globalColor], for: .selected)
        
        let navigationController = UINavigationController(rootViewController: controller)
        
        addChildViewController(navigationController)
    }
    
    
    /// 添加发布按钮
    func addComposeButton () {
        
        tabBar.addSubview(composeButton)
        composeButton.sizeToFit()
        composeButton.frame = tabBar.bounds.insetBy(dx: screenWidth/5 * 2 - 1, dy: 3)
        //
        //        composeButton.center = CGPoint(x: tabBar.bounds.size.width/2, y: tabBar.bounds.size.height/2)
    }
    
    //设置tabbar的背影和阴影
    func setTabarLine () {
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        tabBar.shadowImage = UIImage.createImage(color: UIColor(white: 0.9, alpha: 1.0))
    }
}

// MARK: - 事件处理
extension MainViewController {
    func compose(button: UIButton) {
        print("发布微博")
    }
}






